//
//  TKIMChatViewCell.m
//  TKIM
//
//  Created by liupm on 15/7/27.
//  Copyright (c) 2015年 liupm. All rights reserved.
//

#import "TKIMChatViewCell.h"
#import "TKIMYcSMSMsgView.h"
#import "TKIMChatView.h"
#import "TKIMYCTxtMsgView.h"
//#import "TKIMYCDataParser.h"
//#import "TKIMMessage+TKIMExtMsg.h"

@interface TKIMChatViewCell()
//消息类型
@property(nonatomic,assign) TKIMMsgType msgType;
@end

@implementation TKIMChatViewCell
{
    TKIMMessage* _tempMsg;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  msg:(TKIMMessage *)msg;
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        _msgType = msg.type;
        _tempMsg=msg;
        [self drawView:msg];
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    [self modifiDeleteBtn];
    
    
    if (_tempMsg.channel==2) {
        
        
        if (_isShowSeleted) {
            if(_tempMsg.type==TKIMMsgTypeDTW){
                if (_tempMsg.msgType==TKIMMsgExtYCSMS) {
                    TKIMYcSMSMsgView* msgview=(TKIMYcSMSMsgView*)self.msgView;
                    msgview.isShowSeleted=_isShowSeleted;
                    self.msgView.userInteractionEnabled=NO;
                }else{
                    [self seleteStyle];
                    
                }
            }else{
                [self seleteStyle];
            }
        }else{
            if(_tempMsg.type==TKIMMsgTypeDTW){
                if (_tempMsg.msgType==TKIMMsgExtYCSMS) {
                    TKIMYcSMSMsgView* msgview=(TKIMYcSMSMsgView*)self.msgView;
                    msgview.isShowSeleted=_isShowSeleted;
                    self.msgView.userInteractionEnabled=YES;
                }else{
                    [self noseleteStyle];
                }
            }else{
                [self noseleteStyle];
            }
        }
    }
}

-(void)modifiDeleteBtn{
    for (UIView *subView in self.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
            subView.backgroundColor=[UIColor clearColor];
            for (UIButton *btn in subView.subviews) {
                if ([btn isKindOfClass:[UIButton class]]) {
                    CGRect rect = btn.frame;
                    
                    if(_tempMsg.type==TKIMMsgTypeDTW){
                        if (_tempMsg.msgType==TKIMMsgExtYCSMS) {
                            btn.frame = CGRectMake(rect.origin.x,10,rect.size.width, self.height-20);
                        }
                    }else if (_tempMsg.type ==TKIMMsgTypeTxt){
                        btn.frame = CGRectMake(rect.origin.x, rect.origin.y,rect.size.width, self.height-20);
                    }
                }
            }
        }
    }
}

-(void)noseleteStyle{
    [self.seleteImage removeFromSuperview];
    self.seleteImage=nil;
    self.msgView.origin=CGPointMake(0, 0);
    self.msgView.userInteractionEnabled=YES;
}

-(void)seleteStyle{
    [self.contentView addSubview:self.seleteImage];
    CGFloat height=self.msgView.frame.size.height;
    self.seleteImage.frame = CGRectMake(10, (height-22)*0.5, 22, 22);
    self.msgView.frame = CGRectMake(CGRectGetMaxX(self.seleteImage.frame) + 10, 0, self.msgView.frame.size.width, height);
    self.msgView.userInteractionEnabled=NO;
}


-(UIImageView*)seleteImage{
    if (_seleteImage==nil) {
        _seleteImage=[[UIImageView alloc] init];
        _seleteImage.image=[UIImage imageNamed:@"icon_uncheck"];
    }
    return _seleteImage;
}

//切换选中样式
-(void)choseSeletedImageState:(BOOL)flag{
    if(_tempMsg.type==TKIMMsgTypeDTW){
        if (_tempMsg.msgType==TKIMMsgExtYCSMS) {
            TKIMYcSMSMsgView* msgview=(TKIMYcSMSMsgView*)self.msgView;
            [msgview choseSeletedImageState:flag];
        }else{
            if (flag) {
                self.seleteImage.image=TKIMGetImage(@"icon_checked");
            }else{
                self.seleteImage.image=TKIMGetImage(@"icon_uncheck");
            }
        }
    }else{
        if (flag) {
            self.seleteImage.image=TKIMGetImage(@"icon_checked");
        }else{
            self.seleteImage.image=TKIMGetImage(@"icon_uncheck");
        }
    }
}

-(void)drawView:(TKIMMessage *)msg
{
    switch (_msgType)
    {
        case TKIMMsgTypeTxt:
            if (msg.channel==2) {
                _msgView = [[TKIMYCTxtMsgView alloc] initWithFrame:CGRectMake(0,10, TKIMScreenWidth, self.frame.size.height) msg:msg];
            }else{
                _msgView = [[TKIMTxtMsgView alloc] initWithFrame:CGRectMake(0, 10, TKIMScreenWidth, self.frame.size.height) msg:msg];
            }
            break;
        case TKIMMsgTypeImage:
            _msgView = [[TKIMImageMsgView alloc] initWithFrame:CGRectMake(0, 10, TKIMScreenWidth, self.frame.size.height) msg:msg];
            break;
        case TKIMMsgTypeAudio:
            _msgView = [[TKIMAudioMsgView alloc] initWithFrame:CGRectMake(0, 10, TKIMScreenWidth, self.frame.size.height) msg:msg];
            break;
        case TKIMMsgTypeLocation:
            _msgView = [[TKIMLocationMsgView alloc] initWithFrame:CGRectMake(0, 10, TKIMScreenWidth, self.frame.size.height) msg:msg];
            break;
        case TKIMMsgTypeVideo:
            _msgView = [[TKIMVideoMsgView alloc] initWithFrame:CGRectMake(0, 10, TKIMScreenWidth, self.frame.size.height) msg:msg];
            break;
        case TKIMMsgTypeFile:
            _msgView = [[TKIMFileMsgView alloc] initWithFrame:CGRectMake(0, 10, TKIMScreenWidth, self.frame.size.height) msg:msg];
            break;
        case TKIMMsgTypeRichTxt:
        {
            TKIMMsgExtType type=msg.msgType;
            if (TKIMMsgExtYCSMS==type) {
                _msgView = [[TKIMYcSMSMsgView alloc] initWithFrame:CGRectMake(0, 10, TKIMScreenWidth, [TKIMChatViewCell msgHeight:msg]+25) msg:msg];
            }else{
                _msgView = [[TKIMSrvRichMsgView alloc] initWithFrame:CGRectMake(0, 10, TKIMScreenWidth, self.frame.size.height) msg:msg];
            }
        }
            break;
        case TKIMMsgTypeTemplTxt:
            _msgView = [[TKIMSrvTmplMsgView alloc] initWithFrame:CGRectMake(0, 10, TKIMScreenWidth, self.frame.size.height) msg:msg];
            break;
        case TKIMMsgTypeDTW:{
            TKIMMsgExtType type=msg.msgType;
            if (TKIMMsgExtYCSMS==type) {
                _msgView = [[TKIMYcSMSMsgView alloc] initWithFrame:CGRectMake(0, 10, TKIMScreenWidth, [TKIMChatViewCell msgHeight:msg] + 25) msg:msg];
                UITapGestureRecognizer *toDetail = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toDetail:)];
                _msgView.userInteractionEnabled = YES;
                [_msgView addGestureRecognizer:toDetail];
                self.backgroundColor=[UIColor whiteColor];
            }else{
                _msgView = [[TKIMSrvDtwMsgView alloc] initWithFrame:CGRectMake(0, 10, TKIMScreenWidth, self.frame.size.height) msg:msg];
            }
        }
            break;
        case TKIMMsgTypeScore:
            _msgView = [[TKIMScoreMsgView alloc] initWithFrame:CGRectMake(0, 0, TKIMScreenWidth, self.frame.size.height) msg:msg];
            break;
            
            
        default:
            break;
    }
    [self.contentView addSubview:_msgView];
}


-(void)toDetail:(UITapGestureRecognizer*)tag{
    
    TKIMSrvRichMessage * msg=(TKIMSrvRichMessage*)_msgView.message;
    msg.state = TKIMMsgSendStateRead;
    [[TKIMDBManager shareInstance] updateMsg:msg];
    [self.msgView setMessage:msg];
    //     TKIMMsgClickEvent 所有消息被点击
    [self routerEventWithName:TKIMMsgClickEvent userInfo:msg];
    [self routerEventWithName:TKIMSrvTemplOnclick userInfo:msg.items[0]];
}

/**
 *  @Author lpm, 15-07-27 19:07:37
 *
 *  @brief  赋值
 *
 *  @param message 消息
 */
-(void)setMessage:(id)message
{
    //禁用隐式动画
    [CATransaction begin];
    [CATransaction setDisableActions:YES];
    
    _msgView.message = message;
    
    _tempMsg=message;
    
    self.contentView.frame = CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y,
                                        self.contentView.frame.size.width, _msgView.frame.size.height);
    
    [CATransaction commit];
}



+(CGFloat)msgHeight:(TKIMMessage *)msg
{
    switch (msg.type)
    {
        case TKIMMsgTypeTxt:
        {
            if (2 == msg.channel) {
                return [TKIMYCTxtMsgView heightForRow:msg];
            }else{
                return [TKIMTxtMsgView heightForRow:msg];
            }
        }break;
        case TKIMMsgTypeImage:
            return [TKIMImageMsgView heightForRow:msg];
            break;
        case TKIMMsgTypeAudio:
            return [TKIMAudioMsgView heightForRow:msg];
            break;
        case TKIMMsgTypeLocation:
            return [TKIMLocationMsgView heightForRow:msg];
            break;
        case TKIMMsgTypeVideo:
            return [TKIMVideoMsgView heightForRow:msg];
            break;
        case TKIMMsgTypeFile:
            return [TKIMFileMsgView heightForRow:msg];
            break;
        case TKIMMsgTypeRichTxt:
        {
            
            TKIMMsgExtType type=msg.msgType;
            if (TKIMMsgExtYCSMS==type) {
                
                return [TKIMYcSMSMsgView heightForRow:msg];
                
            }else{
                return [TKIMSrvRichMsgView heightForRow:msg];
            }
            
            
        }
            break;
        case TKIMMsgTypeTemplTxt:
            return [TKIMSrvTmplMsgView heightForRow:msg];
            break;
        case TKIMMsgTypeDTW:
        {
            TKIMMsgExtType type=msg.msgType;
            if (TKIMMsgExtYCSMS==type) {
                return [TKIMYcSMSMsgView heightForRow:msg];
            }else{
                return [TKIMSrvDtwMsgView heightForRow:msg];
            }
            
        }break;
        case TKIMMsgTypeScore:
            return [TKIMScoreMsgView heightForRow:msg];
            break;
        default:
            break;
    }
    return 0;
}

/**
 *  @Author lpm, 15-11-05 09:11:44
 *
 *  @brief  行高
 *
 *  @return
 */
+(CGFloat)heightForRow:(TKIMMessage *)msg
{
    CGFloat height = [self msgHeight:msg]+20;
    msg.size = CGSizeMake(0, height);
    return height;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
